package org.erepublik.company;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Battle implements JSONAble{

    
    private int id;
    private int warId;
    private long battleStartTime;
    private boolean isResistance=false;
    private ECountry attacker;
    private ECountry defender;
    private ArrayList<ECountry> attackersAllies;
    private ArrayList<ECountry> defendersAllies;
    private String region;
    private ArrayList<Hospital> hospitals;    
    
    
    public long getBattleStartTime() {
        return battleStartTime;
    }

    public void setBattleStartTime(long battleStartTime) {
        this.battleStartTime = battleStartTime;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public ECountry getAttacker() {
        return attacker;
    }

    public void setAttacker(ECountry attacker) {
        this.attacker = attacker;
    }

    public ECountry getDefender() {
        return defender;
    }

    public void setDefender(ECountry defender) {
        this.defender = defender;
    }

    public ArrayList<ECountry> getAttackersAllies() {
        return attackersAllies;
    }

    public void setAttackersAllies(ArrayList<ECountry> attackersAllies) {
        this.attackersAllies = attackersAllies;
    }

    public ArrayList<ECountry> getDefendersAllies() {
        return defendersAllies;
    }

    public void setDefendersAllies(ArrayList<ECountry> defendersAllies) {
        this.defendersAllies = defendersAllies;
    }

    public ArrayList<Hospital> getHospitals() {
        return hospitals;
    }

    public void setHospitals(ArrayList<Hospital> hospitals) {
        this.hospitals = hospitals;
    }
    
    
    public static Battle fromJSONObject(JSONObject jsonObject) throws JSONException {
        
        Battle battle = new Battle();
        battle.setId( jsonObject.getInt("id") );
        battle.setWarId( jsonObject.getInt("warId") );
        battle.setResistance( jsonObject.getBoolean("isResistance") );
        battle.setAttacker( ECountry.valueOf( jsonObject.getString("attacker") ) );
        battle.setDefender( ECountry.valueOf( jsonObject.getString("defender") ) );
        battle.setAttackersAllies( getArrayList( jsonObject.getJSONArray("attackersAllies") ) );        
        battle.setDefendersAllies( getArrayList( jsonObject.getJSONArray("defendersAllies") ) );
        battle.setRegion( jsonObject.getString("region") );
        battle.setBattleStartTime( jsonObject.getLong("battleStartTime") );
        
        return battle;
    }
    
    

    private static ArrayList<ECountry> getArrayList(JSONArray jsonArray) throws JSONException {
        ArrayList<ECountry> arrayList = new ArrayList<ECountry>();
        for (int i=0; i < jsonArray.length(); i++) {
            arrayList.add( ECountry.valueOf( jsonArray.getString(i) ) );
        }
        
        return arrayList;
    }

    public JSONObject toJSONObject() throws JSONException {
        
        JSONObject object = new JSONObject();
        object.put("id", id);
        object.put("warId", warId);
        object.put("isResistance", isResistance);
        object.put("attacker", attacker.toString());        
        object.put("defender", defender.toString());    
        object.put("region", region);
        object.put("battleStartTime", battleStartTime);
        object.put("attackersAllies", toJSONArray(attackersAllies));
        object.put("defendersAllies", toJSONArray(defendersAllies));

        return object;
    }

    private JSONArray toJSONArray(ArrayList<ECountry> countries) throws JSONException {
        JSONArray array = new  JSONArray();
        for (int i=0; i < countries.size(); i++) {
            array.put(i, countries.get(i).toString());
        }
        
        return array;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
    
    public ArrayList<Hospital> getNextHospitals() {
        return null;
    }

    public void setWarId(int warId) {
        this.warId = warId;
    }

    public int getWarId() {
        return warId;
    }

    public void setResistance(boolean isResistance) {
        this.isResistance = isResistance;
    }

    public boolean isResistance() {
        return isResistance;
    }

    @Override
    public ECountry getCountry() {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override 
    public boolean equals(Object o) {
	    if (o instanceof Battle) {
		 return ((Battle)o).getId() == getId();   
	    }
	    else 
		    return false;
    }
    
    
    
}
